package com.woniuxy.swagger;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @BelongsProject: Online-education-examination-platform-back
 * @BelongsPackage: com.woniuxy.swagger
 * @Author: Banol Zi
 * @CreateTime: 2022-06-13  14:07
 * @Description: TODO
 * @Version: 1.0
 */
@Configuration
@EnableSwagger2 // Swagger的开关，表示已经启用Swagger
    public class SwaggerConfig {
        @Bean
        public Docket api() {
            Docket docket = new Docket(DocumentationType.SWAGGER_2)
                    .host("localhost:8081")// 不配的话，默认当前项目端口
                    .apiInfo(apiInfo())
                    .pathMapping("/")
                    .select() // 选择哪些路径和api会生成document
//                    .apis(RequestHandlerSelectors.any())// 对所有api进行监控
                .apis(RequestHandlerSelectors.basePackage("com.woniuxy.controller"))// 选择监控的package
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))// 只监控有ApiOperation注解的接口
                    //不显示错误的接口地址
                    .paths(Predicates.not(PathSelectors.regex("/error.*")))//错误路径不监控
                    .paths(PathSelectors.regex("/.*"))// 对根下所有路径进行监控
                    .build();
            return docket;
        }
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder().title("教育考试项目接口文档")
                    .contact(new Contact("Banol Zi", "www.xxx.com", "xxx@gmail.com"))
                    .description("这是用Swagger动态生成的接口文档")
                    .termsOfServiceUrl("NO terms of service")
                    .license("The Apache License, Version 2.0")
                    .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                    .version("1.0")
                    .build();
        }
}


